<template>
  <el-dialog :visible="value" :before-close="cancel">
    <template slot="title">编辑手续费方案</template>
    <el-form :model="form" :rules="refRules" ref="form" label-width="150px">
      <el-form-item label="手续费方案">
        <el-select v-model="form.ruleId">
          <el-option
            v-for="item in modelList"
            :label="item.name"
            :value="item.id"
            :key="item.id"
            :disabled="item.status == 1"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { form, grid, factory } from '@/build';
import { getCommissionList } from '@/services/api/commission';
import { updateAgencyCommission } from '@/services/api/agency';

export default {
  name: 'editing-fee-plan',
  props: ['value', 'id', 'rowData'],
  mixins: [form, grid, factory],
  data() {
    return {
      modelList: [],
      form: {
        proxyId: this.id,
        ruleId: (this.rowData && this.rowData.feeRuleNextId) ? this.rowData.feeRuleNextId : null,
        rebate: 1,
        proxy: 'admin',
      },
      refRules: {
        ruleId: { required: true, message: '手续费方案为必选项' },
      },
    };
  },
  methods: {
    cancel() {
      this.$emit('input', false);
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.doSave({ action: 'saveForm', form: formName });
          this.cancel();
        }
      });
    },
  },
  mounted() {
    this.bindGet(getCommissionList, 'modelList');
    this.getAll();
    this.bindSave(updateAgencyCommission, 'saveForm', 'form', {
      beforeSend: data => ({
        ...data,
        uid: this.id,
      }),
      afterSuccess: () => {
        setTimeout(() => {
          this.$parent.getAll();
        }, 2000);
      },
    });
  },
};
</script>

<style lang="scss" scoped>
</style>
